package what.the.fun.dao;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import what.the.fun.domain.Campaign;
import what.the.fun.jdbc.CampaignRowMapper;

public class CampaignDAOImpl implements CampaignDAO{

	@Autowired
	DataSource dataSource;
	JdbcTemplate jdbcTemplate;
	String sql;
	
	@Override
	public List<Campaign> getDataList() {
		sql = "call sp_campaign_GetAll ()";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		List<Campaign> campList = new ArrayList<Campaign>();
		campList = jdbcTemplate.query(sql, new CampaignRowMapper());
		
		return campList;
	}

	@Override
	public void insertData(Campaign campaign) {
		sql = "call sp_campaign_Insert (?, ?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		jdbcTemplate.update(sql, campaign.getName(), campaign.getUserId());
	}

	@Override
	public void updateData(Campaign campaign) {
		sql = "call sp_campaign_Update (?, ?, ?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		jdbcTemplate.update(sql, campaign.getName(), campaign.getUserId(), campaign.getCampId());
	}

	@Override
	public void deleteData(String id) {
		sql = "call sp_campaign_Delete (?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		jdbcTemplate.update(sql, id);
	}

	@Override
	public Campaign getData(String id) {
		sql = "call sp_campaign_GetById (?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		List<Campaign> campList = new ArrayList<Campaign>();
		campList = jdbcTemplate.query(sql, new CampaignRowMapper(), id);
		
		return campList.get(0);
	}

}
